
package hcn.sync.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 项目名称：etl3.0 类名称：StringUtil 类描述： 格式化工具类 创建人：chase 创建时间：2014年6月11日 下午5:18:51
 * 修改人：chase 修改时间：2014年6月11日 下午5:18:51 修改备注：
 */
public class StringUtils {

    private static StringUtils instance;


    public static String nullToSpace(String paramString) {
        if (paramString == null)
            return "";
        return paramString;
    }

    public static String spaceToBlank(String paramString) {
        if ("".equals(nullToSpace(paramString)))
            return " ";
        return paramString;
    }

    public static String ListToString(String[] paramArrayOfString, String paramString) {
        if (paramArrayOfString == null)
            return "";
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < paramArrayOfString.length; i++)
            if (i == 0)
                localStringBuffer.append(paramArrayOfString[i]);
            else
                localStringBuffer.append(new StringBuilder().append(paramString).append(paramArrayOfString[i]).toString());
        return localStringBuffer.toString();
    }

    public static String ListToString(List<String> paramList, String paramString) {
        if (paramList == null)
            return "";
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < paramList.size(); i++)
            if (i == 0)
                localStringBuffer.append((String) paramList.get(i));
            else
                localStringBuffer.append(new StringBuilder().append(paramString).append((String) paramList.get(i)).toString());
        return localStringBuffer.toString();
    }

    public static String ListToStringAdd(String[] paramArrayOfString, String paramString) {
        if (paramArrayOfString == null)
            return "";
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < paramArrayOfString.length; i++)
            if (i == 0)
                localStringBuffer.append(new StringBuilder().append("'").append(paramArrayOfString[i]).append("'").toString());
            else
                localStringBuffer.append(new StringBuilder().append(paramString).append("'").append(paramArrayOfString[i]).append("'").toString());
        return localStringBuffer.toString();
    }

    public static String ListToStringAdd(List<?> paramList, String paramString1, String paramString2) {
        if (paramList == null)
            return "";
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < paramList.size(); i++) {
            Map localMap = (Map) paramList.get(i);
            String str = localMap.get(paramString2) == null ? "" : localMap.get(paramString2).toString();
            String[] arrayOfString = str.split(paramString1);
            for (int j = 0; j < arrayOfString.length; j++)
                if ("".equals(localStringBuffer.toString()))
                    localStringBuffer.append(new StringBuilder().append("'").append(arrayOfString[j]).append("'").toString());
                else
                    localStringBuffer.append(new StringBuilder().append(paramString1).append("'").append(arrayOfString[j]).append("'").toString());
        }
        if ("".equals(localStringBuffer.toString()))
            localStringBuffer.append("''");
        return localStringBuffer.toString();
    }

    public static Date StringToDate(String paramString1, String paramString2) {
        if ((paramString1 != null) && (!"".equals(paramString1))) {
            SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2);
            Date localDate = null;
            try {
                localDate = localSimpleDateFormat.parse(paramString1);
            } catch (ParseException localParseException) {
                localParseException.printStackTrace();
            }
            return localDate;
        }
        return null;
    }

    public static String DateToString(Date paramDate, String paramString) {
        String str = "";
        if (paramDate != null) {
            SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
            str = localSimpleDateFormat.format(paramDate);
        }
        return str;
    }

    public static int getBetweenDays(Date paramDate1, Date paramDate2) {
        Calendar localCalendar1 = Calendar.getInstance();
        Calendar localCalendar2 = Calendar.getInstance();
        localCalendar1.setTime(paramDate1);
        localCalendar2.setTime(paramDate2);
        int i = localCalendar2.get(1) - localCalendar1.get(1);
        int j = localCalendar2.get(6) - localCalendar1.get(6);
        for (int k = 0; k < i; k++) {
            localCalendar1.set(1, localCalendar1.get(1) + 1);
            j += localCalendar1.getActualMaximum(6);
        }
        return j;
    }

    public static Date getNextDate(Date paramDate, int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(5);
        localCalendar.set(5, i + paramInt);
        return localCalendar.getTime();
    }

    public static Date getBeforeDate(Date paramDate, int paramInt) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(5);
        localCalendar.set(5, i - paramInt);
        return localCalendar.getTime();
    }

    public static List<Date> getDateList(String paramString1, String paramString2) {
        ArrayList localArrayList = new ArrayList();
        Date localDate1 = StringToDate(paramString1, "yyyy-MM-dd");
        Date localDate2 = StringToDate(paramString2, "yyyy-MM-dd");
        for (Date localDate3 = localDate1; localDate3.before(localDate2); localDate3 = getNextDate(localDate3, 1))
            localArrayList.add(localDate3);
        return localArrayList;
    }

    public static Date getEndOfMonth(Date paramDate) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        localCalendar.set(5, 1);
        localCalendar.roll(5, -1);
        return localCalendar.getTime();
    }

    public static boolean isRemind(Date paramDate, int paramInt) {
        boolean isOk = false;
        paramDate = getBeforeDate(paramDate, paramInt);
        if (paramDate.compareTo(new Date()) >= 0)
            isOk = true;
        return isOk;
    }

    public static boolean isWeekEnd(Date paramDate) {
        boolean isOk = false;
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int j = localCalendar.get(7);
        if ((j == 1) || (j == 7))
            isOk = true;
        return isOk;
    }

    public static String createNumberString(int paramInt) {
        StringBuffer localStringBuffer = new StringBuffer();
        Random localRandom = new Random();
        for (int i = 0; i < paramInt; i++)
            localStringBuffer.append("0123456789".charAt(localRandom.nextInt("0123456789".length())));
        return localStringBuffer.toString();
    }

    public static boolean ipIsValid(String paramString1, String paramString2) {
        if (paramString1 == null)
            throw new NullPointerException("IP段不能为空！");
        if (paramString2 == null)
            throw new NullPointerException("IP不能为空！");
        paramString1 = paramString1.trim();
        paramString2 = paramString2.trim();
        if ((!paramString1.matches("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\-((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)")) || (!paramString2.matches("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)")))
            return false;
        int i = paramString1.indexOf(45);
        String[] arrayOfString1 = paramString1.substring(0, i).split("\\.");
        String[] arrayOfString2 = paramString1.substring(i + 1).split("\\.");
        String[] arrayOfString3 = paramString2.split("\\.");
        long l1 = 0L;
        long l2 = 0L;
        long l3 = 0L;
        for (int j = 0; j < 4; j++) {
            l1 = l1 << 8 | Integer.parseInt(arrayOfString1[j]);
            l2 = l2 << 8 | Integer.parseInt(arrayOfString2[j]);
            l3 = l3 << 8 | Integer.parseInt(arrayOfString3[j]);
        }
        if (l1 > l2) {
            long l4 = l1;
            l1 = l2;
            l2 = l4;
        }
        return (l1 <= l3) && (l3 <= l2);
    }

    public static String changeNumToDate(String paramString) {
        String str = "1900-01-01";
        try {
            SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date localDate = new Date();
            localDate = localSimpleDateFormat.parse("1900-01-01");
            long l = localDate.getTime();
            l = l / 1000L + (Long.parseLong(paramString) - 2L) * 24L * 3600L;
            localDate.setTime(l * 1000L);
            str = localSimpleDateFormat.format(localDate);
        } catch (Exception localException) {
            str = "1900-01-01";
        }
        return str;
    }

    public static String toUNICODE(String paramString) {
        if (null == paramString)
            return null;
        StringBuilder localStringBuilder = new StringBuilder();
        for (int i = 0; i < paramString.length(); i++) {
            if (paramString.charAt(i) <= 'Ā')
                localStringBuilder.append("\\u00");
            else
                localStringBuilder.append("\\u");
            localStringBuilder.append(Integer.toHexString(paramString.charAt(i)));
        }
        return localStringBuilder.toString();
    }

    public static String UnicodeToString(String paramString) {
        Pattern localPattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher localMatcher = localPattern.matcher(paramString);
        while (localMatcher.find()) {
            char c = (char) Integer.parseInt(localMatcher.group(2), 16);
            paramString = paramString.replace(localMatcher.group(1), new StringBuilder().append(c).append("").toString());
        }
        return paramString;
    }

    public static String getDateTime(String paramString) {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTimeInMillis(Long.parseLong(paramString));
        return DateToString(localCalendar.getTime(), "yyyy-MM-dd");
    }

    public static boolean isNumeric(String paramString) {
        Pattern localPattern = Pattern.compile("[0-9]*");
        Matcher localMatcher = localPattern.matcher(paramString);
        return (localMatcher.matches()) && (!"".equals(paramString));
    }

    public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
        if (cs1 == cs2) {
            return true;
        }
        if (cs1 == null || cs2 == null) {
            return false;
        }
        if (cs1 instanceof String && cs2 instanceof String) {
            return cs1.equals(cs2);
        }
        return regionMatches(cs1, false, 0, cs2, 0, Math.max(cs1.length(), cs2.length()));
    }

    static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
                                 final CharSequence substring, final int start, final int length) {
        if (cs instanceof String && substring instanceof String) {
            return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
        } else {
            int index1 = thisStart;
            int index2 = start;
            int tmpLen = length;

            while (tmpLen-- > 0) {
                char c1 = cs.charAt(index1++);
                char c2 = substring.charAt(index2++);

                if (c1 == c2) {
                    continue;
                }

                if (!ignoreCase) {
                    return false;
                }

                // The same check as in String.regionMatches():
                if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
                        && Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                    return false;
                }
            }
            return true;
        }
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }
}